package _24define;

import java.io.*;


public class Demo {
    public static void main(String[] args) throws IOException {
        String sourcePath1 = "src/note/a.txt";
        String targetPath1 = "src/note/copya.txt";
        String sourcePath2 = "src/note/b.txt";
        String targetPath2 = "src/note/copyb.txt";
        String sourcePath3 = "src/note/c.txt";
        String targetPath3 = "src/note/copyc.txt";
        FileCopyTask fileCopyTask1 = new FileCopyTask(sourcePath1,targetPath1);
        FileCopyTask fileCopyTask2 = new FileCopyTask(sourcePath2,targetPath2);
        FileCopyTask fileCopyTask3 = new FileCopyTask(sourcePath3,targetPath3);
        fileCopyTask1.run();
        fileCopyTask2.run();
        fileCopyTask3.run();
        System.out.println("所有文件都已复制完成");

    }
}
class FileCopyTask implements Runnable{
    private String sourcePath;
    private String targetPath;

    public FileCopyTask(String sourcePath, String targetPath) {
        this.sourcePath = sourcePath;
        this.targetPath = targetPath;
    }

    @Override
    public void run() {
        try {
            FileInputStream in=new FileInputStream(sourcePath);
            FileOutputStream out=new FileOutputStream(targetPath);
            double allBytes= in.available();
            byte[] bytes=new byte[128];
            int bytesRead;
            long totalBytesCopied=0;
            System.out.println(in.available());
            while ((bytesRead=in.read(bytes))!=-1){
                out.write(bytes,0,bytesRead);
                totalBytesCopied+=bytesRead;
                double progress=(double) totalBytesCopied/allBytes*100;
                System.out.println(sourcePath+"文件已复制"+progress+"%");
            }
            in.close();
            out.close();
            System.out.println(sourcePath+"文件已复制完成");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
